package cn.edu.cug.cs.gtl.plugins.device.dialog;

import javax.swing.*;
import java.awt.*;

public class StorageClusterDialog extends JDialog {
    private JTextField connectionNameField = new JTextField();
    private JTextField urlField = new JTextField();
    private JTextField userField = new JTextField();
    private JPasswordField passwordField = new JPasswordField();

    // 表示用户是否点击了确认按钮
    private boolean isConfirmed = false;

    public StorageClusterDialog(JFrame parent) {
        super(parent, "存储集群配置", true);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(5, 5, 5, 5);

        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(new JLabel("ConnectionName:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        connectionNameField.setColumns(20); // 设置输入框的列数
        panel.add(connectionNameField, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        panel.add(new JLabel("URL:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        urlField.setColumns(20);
        panel.add(urlField, constraints);

        constraints.gridx = 0;
        constraints.gridy = 2;
        panel.add(new JLabel("User:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 2;
        userField.setColumns(20);
        panel.add(userField, constraints);

        constraints.gridx = 0;
        constraints.gridy = 3;
        panel.add(new JLabel("Password:"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 3;
        passwordField.setColumns(20);
        panel.add(passwordField, constraints);


        // 创建一个名为 buttonPanel 的新面板，用于容纳 Confirm 和 Cancel 按钮。
        // 使用不同的布局约束 buttonConstraints 来放置这两个按钮，并将 buttonPanel 放置在输入框面板中的最后一行。
        JPanel buttonPanel = new JPanel(new GridBagLayout());
        GridBagConstraints buttonConstraints = new GridBagConstraints();
        buttonConstraints.insets = new Insets(5, 5, 5, 5);

        JButton confirmButton = new JButton("Confirm");
        JButton cancelButton = new JButton("Cancel");

        confirmButton.addActionListener(e -> {
            isConfirmed = true;
            setVisible(false);
        });

        cancelButton.addActionListener(e -> {
            isConfirmed = false;
            setVisible(false);
        });

        buttonConstraints.gridx = 0;
        buttonConstraints.gridy = 0;
        buttonPanel.add(confirmButton, buttonConstraints);

        buttonConstraints.gridx = 1;
        buttonConstraints.gridy = 0;
        buttonPanel.add(cancelButton, buttonConstraints);

        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 2;
        panel.add(buttonPanel, constraints);

        add(panel);
        pack();
        setSize(500, 250); // 设置窗口大小
        setLocationRelativeTo(parent);
    }

    public String getConnectionName() {
        return connectionNameField.getText();
    }

    public String getUrl() {
        return urlField.getText();
    }

    public String getUser() {
        return userField.getText();
    }

    public String getPassword() {
        return String.valueOf(passwordField.getPassword());
    }

    @Override
    public void setVisible(boolean visible) {
        super.setVisible(visible);
        if (!visible) {
            // Clear the password field when the dialog is closed
            // passwordField.setText(null);
        }
    }

    public boolean isConfirmed() {
        return isConfirmed;
    }

    public void setInitialValues(String connectionName, String url, String user, String password) {
        connectionNameField.setText(connectionName);
        urlField.setText(url);
        userField.setText(user);
        passwordField.setText(password);
    }


}
